package com.myh.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.myh.gulimall.product.dao.AttrDao;
import com.myh.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.myh.gulimall.product.entity.AttrEntity;
import com.myh.gulimall.product.service.AttrAttrgroupRelationService;
import com.myh.gulimall.product.service.AttrService;
import com.myh.gulimall.product.vo.AttrGroupRelationVo;
import com.myh.gulimall.product.vo.AttrGroupWithAttrVo;
import com.myh.gulimall.product.vo.AttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myh.common.utils.PageUtils;
import com.myh.common.utils.Query;

import com.myh.gulimall.product.dao.AttrGroupDao;
import com.myh.gulimall.product.entity.AttrGroupEntity;
import com.myh.gulimall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.eq("attr_group_id", key).or().like("attr_group_name", key);
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        } else {
            queryWrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public String getNameByAttrGroupId(Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
        return attrGroupEntity.getAttrGroupName();
    }

    @Override
    @Transactional
    public List<AttrVo> getAttrgroupRelAttrs(Long attrgroupId) {
        List<Long> attrIds = attrAttrgroupRelationService.getAttrIdsByAttrgroupId(attrgroupId);
        if (attrIds.size() != 0) {
            List<AttrEntity> attrEntities = attrDao.selectBatchIds(attrIds);
            List<AttrVo> attrVos = attrEntities.stream().map((entity) -> {
                AttrVo attrVo = new AttrVo();
                BeanUtils.copyProperties(entity, attrVo);
                return attrVo;
            }).collect(Collectors.toList());
            return attrVos;
        } else {
            return null;
        }

    }

    @Override
    public Long getCatelogIdByAttrGroupId(Long attrgroupId) {
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
        if (!Objects.isNull(attrGroupEntity)) {
            return attrGroupEntity.getCatelogId();
        } else {
            return null;
        }

    }

    @Override
    public void addRelation(List<AttrGroupRelationVo> vos) {
        vos.stream().map((vo) -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(vo, entity);
            Integer count = attrAttrgroupRelationService.selectCountByAttrId(entity.getAttrId());
            if (count == 0){
                //做添加操作
                attrAttrgroupRelationService.save(entity);
            }else {
                //做修改操作
                attrAttrgroupRelationService.update(entity, new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", entity.getAttrId()));
            }
            return entity;
        }).collect(Collectors.toList());
    }

    @Override
    public List<AttrGroupWithAttrVo> getAllAttrGroupWithAttr(Long catelogId) {
        List<AttrGroupEntity> groupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrVo> collect = groupEntities.stream().map((entity) -> {
            AttrGroupWithAttrVo attrGroupWithAttrVo = new AttrGroupWithAttrVo();
            BeanUtils.copyProperties(entity, attrGroupWithAttrVo);
            List<Long> attrIds = attrAttrgroupRelationService.getAttrIdsByAttrgroupId(entity.getAttrGroupId());
            List<AttrEntity> attrEntities = attrDao.selectBatchIds(attrIds);
            attrGroupWithAttrVo.setAttrs(attrEntities);
            return attrGroupWithAttrVo;
        }).collect(Collectors.toList());
        return collect;
    }

}